import DrawBaseItem from './DrawBaseItem';
import {Point, Polygon} from 'pixi.js';

/**
 * 线
 *
 * LineItem
 * 
 * @extends DrawBaseItem
 * @class
 * @memberof Nice.DrawBase
 * @author iwangcj
 */
export default class LineItem extends DrawBaseItem 
{
    constructor(pt1 = new Point(0,0), pt2 = new Point(0,0)) {
        super();

        this.linePath = new Polygon(pt1, pt2);
        this.linePath.closeStroke = false;
    }


    get startPoint() {
        const points = super.currentPath.points;

        return new Point(points[0], points[1]);
    }

    set startPoint(point) {
        if (point && point instanceof Point) {
            const points = this.linePath.points;
            points[0] = point.x;
            points[1] = point.y;
        }
    }

    setStartPoint(x, y) {
        if (x && x instanceof Number && y && y instanceof Number) {
            const points = this.linePath.points;
            points[0] = x;
            points[1] = y;
        }
    }
    
    get endPoint() {
        const points = this.linePath.points;

        return new Point(points[2], points[3]);
    }

    set endPoint(point) {
        if (point && point instanceof Point) {
            const points = this.linePath.points;
            points[2] = point.x;
            points[3] = point.y;
        }
    }

    setEndPoint(x, y) {
        if (x && x instanceof Number && y && y instanceof Number) {
            const points = this.linePath.points;
            points[2] = x;
            points[3] = y;
        }
    }

    draw() {
        this.drawShape(this.linePath);
    }

    /**
     * get/set 属性
     * @param {*} key 
     * @param {*} value 
     */
    attr(key, value) {
        if (undefined != value) {
            let isOk = this.attrBase(key, value);
            !isOk && (isOk = this.attrStyle(key, value)); 

            if (isOk) {
                this.draw();
            }
        }
        else {
            let result = this.attrBase(key);
            undefined == result && (result = this.attrStyle(key));
            return result;
        }
    }
}